Skip to content

Add Nemotron-Labs-Diffusion architecture support - #1918

Closed
12ohan wants to merge 1 commit into
ml-explore:mainfrom
12ohan2:add-nemotron-labs-diffusion
Closed

12ohan wants to merge 1 commit into
ml-explore:mainfrom
12ohan2:add-nemotron-labs-diffusion

Conversation

@12ohan

@12ohan 12ohan commented Sep 23, 2026

Copy link
Copy Markdown

Summary

Nemotron-Labs-Diffusion (NLD) is NVIDIA's tri-mode language model family (3B/8B/14B). This PR adds MLX support for NLD in standard autoregressive (AR) mode.

The AR backbone shares geometry with `ministral3` (GQA, RMSNorm, YaRN rotary embeddings) with `tie_word_embeddings: false`, giving an untied `lm_head` (`Linear(3072, 131072)`) totaling 3,831,659,520 parameters for the 3B model. The output projection in the checkpoint is named `diffusion_head` instead of `lm_head`. NLD was continuously pretrained from Ministral-3B base on 1.3T additional tokens.

Changes

  • Implements `mlx_lm/models/nemotron_labs_diffusion.py` subclassing `ministral3.Model`.
  • `sanitize()` automatically:
    1. Maps `diffusion_head.{weight,scales,biases}` to `lm_head.{weight,scales,biases}`.
    2. Maps `encoder.{layers,embed_tokens,norm}` to `model.{layers,embed_tokens,norm}` (resolving 237/237 checkpoint keys with zero missing and zero extra).
    3. Strips legacy `language_model.` prefixes present in community checkpoints.
  • Adds test configuration to `test_all_models` and dedicated test in `tests/test_models.py`.

Evidence & Implementation Receipts

  1. Parity Against PyTorch MPS Reference:

    • Greedy decode is token-exact (96/96 tokens, 3 prompts x 32 steps) vs a PyTorch MPS fp16 reference run in AR mode (`diffusion_lm=False`, per NVIDIA's `ar_generate`).
    • Max |Δlogits| <= 1.4e-1 (mean <= 3.2e-2), consistent with fp16 kernel reduction noise.
    • Cache-offset self-parity is bit-exact (0.000e+00, verified sensitive to errors via off-by-one mutation testing yielding Δ = 6.29).
    • Verified below the 16k original context horizon; >16k YaRN scaling untested and scoped for follow-up.
  2. Parameter Accounting (Untied lm_head):

    • Ministral-3B base (`tie_word_embeddings: true`): 3,026,353,152 (layers) + 402,653,184 (tied embeddings) = 3,429,006,336 parameters.
    • NLD-3B (`tie_word_embeddings: false`): Adds distinct `lm_head` (`Linear(3072, 131072)`) of 402,653,184 parameters:
      3,429,006,336 + 402,653,184 = 3,831,659,520 parameters.
    • Verified that `model.embed_tokens.weight` and `lm_head.weight` are distinct tensors in memory.
  3. YaRN Configuration Surface Alignment:

    • NLD config carries:
      ```json
      "rope_parameters": {
      "beta_fast": 32.0,
      "beta_slow": 1.0,
      "factor": 16.0,
      "llama_4_scaling_beta": 0.1,
      "mscale": 1.0,
      "mscale_all_dim": 1.0,
      "original_max_position_embeddings": 16384,
      "rope_theta": 1000000.0,
      "rope_type": "yarn"
      }
      ```
    • Both PyTorch (`modeling_ministral.py` line 105) and MLX (`ministral3.py` line 48) implement `_get_llama_4_attn_scale` in identical floor form:
      `scaling = 1 + beta * log(1 + floor(pos / max_position_embeddings))`
    • Evaluated on `[0, 511, 16383, 16384, 32768]` on-device (fp32): matches bit-for-bit between PyTorch and MLX. Below 16,384 tokens, `attn_scale` evaluates to exactly 1.0.
    • MLX `rope_utils.YarnRoPE` directly consumes `beta_fast`, `beta_slow`, `mscale`, and `mscale_all_dim`.
  4. Direct Lineage Corroboration:

    • In NVIDIA's reference implementation, the base modeling file is literally titled `modeling_ministral.py`, providing source-level corroboration for subclassing MLX's `ministral3.Model`.
  5. Unit Tests:

    • Unit tests passing on Apple Silicon M2 Pro:
      • `test_nemotron_labs_diffusion`: verifies untied weights, key sanitization across native, raw NLD (`encoder.` / `diffusion_head.`), and community repack layouts.
      • `test_all_models`: verifies forward passes across fp32 and fp16, prompt caching, cache decode, and batched execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant